<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Feed_parser{

    public $feed_uri;

    function __construct() {
          $this->CI =& get_instance();
    }

    function parse($uri) {
      //Parse the document
      $rawFeed = $this->CI->curl_post->get($uri);
      #$rawFeed = file_get_contents($uri);
      try {
		    $xml = new SimpleXmlElement($rawFeed, LIBXML_NOERROR);
            foreach ($xml->channel->item as $item){
               $data = array();
               $data['title'] = $item->title;
               $data['description'] = $item->description;
               $data['keywords'] = strip_tags($item->description);
               if($item->PUBDATE){
                   $data['pubdate'] = $item->PUBDATE;
               }else{
                   $data['pubdate'] = $item->pubdate;
               }
               $data['link'] = $item->link;
               if($item->category){
                   $data['category'] = $item->category;
               }
               $res[] = $data;
            }
	    } catch (Exception $e) {
		    echo 'Caught exception: ',  $e->getMessage(), "<br />\n";
		}
      

      //Assign the channel data
      $this->channel_data['title'] = $xml->channel->title;
      $this->channel_data['description'] = $xml->channel->description;

      return $res;
  }
  
}

?>